﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using System.Windows.Media;

namespace Demo01_Test.Models
{
    public class LinkBase : SharpBase
    {
        public LinkBase(PortBase sourcePort, PortBase targetPort)
        {
            SourcePort = sourcePort;
            TargetPort = targetPort;

            SourcePort.PropertyChanged += Port_PropertyChanged;
            TargetPort.PropertyChanged += Port_PropertyChanged;
            Update();
        }

        private Geometry _geometry;
        public Geometry Geometry
        {
            get
            {
                return _geometry;
            }
            set
            {
                SetProperty(ref _geometry, value);
            }
        }

        private PortBase _sourcePort;
        public PortBase SourcePort
        {
            get
            {
                return _sourcePort;
            }
            set
            {
                SetProperty(ref _sourcePort, value);

            }
        }

        private PortBase _targetPort;
        public PortBase TargetPort
        {
            get
            {
                return _targetPort;
            }
            set
            {
                SetProperty(ref _targetPort, value);
            }
        }

        private void Port_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Update();
        }

        public void Update()
        {
            var geometry = new PathGeometry();
            var figure = new PathFigure { StartPoint = SourcePort.Position };
            geometry.Figures.Add(figure);

            LineSegment arc = new LineSegment(TargetPort.Position, true) { IsSmoothJoin = true };
            geometry.Figures[0].Segments.Add(arc);
            Geometry = geometry;
            Left = Geometry.Bounds.Left;
            Top = Geometry.Bounds.Top;
            Width = Geometry.Bounds.Width;
            Height = Geometry.Bounds.Height;
        }
    }
}
